﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;

using System.Windows.Forms;
using HowtoScene.OpenGL;
namespace HowtoScene.OpenGLUT
{
  public partial class CH03 : OpenGLControl
  {
    public CH03()
    {
      InitializeComponent();
     
    }

   
    protected override void OnCreateControl()
    {
      GL.glClearDepth(1.0);
      GL.glDepthFunc(GL.GL_LEQUAL);
      GL.glEnable(GL.GL_DEPTH_TEST);
      GL.glShadeModel(GL.GL_SMOOTH);
      GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    }
    public override void glDraw()
    {
      
      GL.glClearColor(0, 0, 0, 1);
      GL.glColor3f(0, 1, 0);
      GL.glClear(Win32GL.GL_COLOR_BUFFER_BIT);
      //绕坐标轴旋转
      GL.glRotated(-90, 1, 0, 0);
      GL.glRotated(30, 0, 0, 1);
      //首先将原点移动(-15,0,0)
      GL.glTranslatef(-15F, 0, 0);
      //绘制一个三角形
      GL.glBegin(GL.GL_TRIANGLES);
      GL.glVertex3f(-30, 0,-20);
      GL.glVertex3f(30, 0, -20);
      GL.glVertex3f(0, 0, 20);
      GL.glEnd();
      //将原点移动60,0,0
      GL.glTranslatef (60,0,0);
      //绘制四边形
      GL.glBegin(GL.GL_QUADS);
      GL.glVertex3f(-20, 0, -20);
      GL.glVertex3f(20, 0, -20);
      GL.glVertex3f(20, 0, 20);
      GL.glVertex3f(-20, 0, 20);
      GL.glEnd();
      
    }
    protected override void OnSizeChanged(EventArgs e)
    {      
      base.OnSizeChanged(e);
      float range = 100.0f;
      int width = this.Size.Width;
      int height = this.Size.Height;
      //设置视口
      GL.glViewport(0, 0, width, height);
      //下面开始设置投影矩阵
      GL.glMatrixMode(Win32GL.GL_PROJECTION);
      GL.glLoadIdentity();   
      //保证x/y比例和控件的宽高比例一致,避免变形.
      if (width < height)
      {
        GL.glOrtho(-range, range, -range * height / width,
          range * height / width, -range, range);
      }
      else
      {
        GL.glOrtho(-range * width / height, range * width / height,
          -range, range, -range, range);
      }
      //设置模型视图矩阵
      GL.glMatrixMode(Win32GL.GL_MODELVIEW);
      GL.glLoadIdentity();
    }
  }
}
